package webservice;

import ejb.facades.forum.PostFacadeLocal;
import ejb.facades.forum.TopicFacadeLocal;
import entities.forum.Post;
import entities.forum.Topic;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import sessions.IUserAccountSession;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@WebService(name = "ForumWebService", serviceName = "ForumWebService")
public class ForumWebService {

    @Inject
    private IUserAccountSession userAccountSession;
    @EJB
    private PostFacadeLocal postFacade;
    @EJB
    private TopicFacadeLocal topicFacade;

    @WebMethod(operationName = "getAllTopics")
    public String getAllTopics() {
        List<Topic> allTopics = topicFacade.findAll();
        if (!allTopics.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Topic t : allTopics) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_topic", t.getId())
                        .add("message", t.getMessage())
                        .add("title", t.getTitle())
                        .add("id_user", t.getUser().getId())
                        .add("name_user", t.getUser().getName());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("topic", createArrayBuilder.build()).build().toString();
        }
        return "{\"topic\":[]}";
    }

    @WebMethod(operationName = "getRecentTopics")
    public String getRecentTopics(@WebParam(name = "top") int top) {
        List<Topic> allTopics = topicFacade.findRecent(top);
        if (!allTopics.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Topic t : allTopics) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_topic", t.getId())
                        .add("message", t.getMessage())
                        .add("title", t.getTitle())
                        .add("id_user", t.getUser().getId())
                        .add("name_user", t.getUser().getName());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("topic", createArrayBuilder.build()).build().toString();
        }
        return "{\"topic\":[]}";
    }

    @WebMethod(operationName = "getPostsFromTopic")
    public String getPostsFromTopic(@WebParam(name = "topic_id") int topic_id) {
        Topic find = topicFacade.find(topic_id);
        if (find != null) {
            List<Post> postsFromTopic = postFacade.getPostsFromTopic(find);
            if (!postsFromTopic.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Post p : postsFromTopic) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_post", p.getId())
                            .add("message", p.getMessage())
                            .add("post_number", p.getPostNumber())
                            .add("id_user", p.getUser().getId())
                            .add("name_user", p.getUser().getName())
                            .add("id_topic", p.getTopic().getId())
                            .add("title_topic", p.getTopic().getTitle());

                    createArrayBuilder.add(add.build());
                }

                return Json.createObjectBuilder().add("topic", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"topic\":[]}";
    }

    @WebMethod(operationName = "getTopic")
    public String getTopic(@WebParam(name = "topic_id") int topic_id) {
        Topic t = topicFacade.find(topic_id);
        if (t == null) {
            return "{}";
        } else {
            return Json.createObjectBuilder()
                    .add("id_topic", t.getId())
                    .add("message", t.getMessage())
                    .add("created_date", t.getCreatedDate().toString())
                    .add("posts_size", t.getPosts().size())
                    .add("title", t.getTitle())
                    .add("id_user", t.getUser().getId())
                    .add("name_user", t.getUser().getName())
                    .build().toString();
        }
    }

    @WebMethod(operationName = "getPost")
    public String getPost(@WebParam(name = "post_id") int post_id) {
        Post p = postFacade.find(post_id);
        if (p == null) {
            return "{}";
        } else {
            return Json.createObjectBuilder()
                    .add("id_post", p.getId())
                    .add("message", p.getMessage())
                    .add("post_number", p.getPostNumber())
                    .add("created_date", p.getCreatedDate().toString())
                    .add("id_user", p.getUser().getId())
                    .add("name_user", p.getUser().getName())
                    .add("id_topic", p.getTopic().getId())
                    .add("title_topic", p.getTopic().getTitle())
                    .build().toString();
        }
    }

    @WebMethod(operationName = "createTopic")
    public String createTopic(@WebParam(name = "title") String title, @WebParam(name = "message") String message) {
        return "{\"success\":" + userAccountSession.createTopic(title, message) + "}";
    }

    @WebMethod(operationName = "post")
    public String post(@WebParam(name = "topic_id") int topic_id, @WebParam(name = "message") String message) {
        return "{\"success\":" + userAccountSession.post(topic_id, message) + "}";
    }
    
    /*@WebMethod(operationName = "deleteTopic")
    public String deleteTopic(@WebParam(name = "id_topic") int id_topic) {
        return "{\"success\":" + (userAccountSession.deleteTopic(id_topic) ? "true" : "false") + "}";
    }*/
}
